﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ServiceRepository.DataModel.Mapping
{
    public class OperationMapper : BaseMapper<Operation, Domain.Operation>
    {
        protected override Domain.Operation FromData(Operation data)
        {
            var auxParameters = (from q in data.Parameters select new KeyValuePair<String, Type>(q.Name, Type.GetType(q.FQN)));
            Dictionary<String, Type> parameters = new Dictionary<string, Type>();
            foreach (var param in auxParameters)
            {
                parameters.Add(param.Key, param.Value);
            }
            return new Domain.Operation() { ID = data.ID, Name = data.Name, ReturnType = Type.GetType(data.ReturnType), Parameters = parameters };
        }

        protected override Operation FromDomain(Domain.Operation model)
        {
            Operation entity = (from obj in _ctx.Operations where obj.ID == model.ID select obj).FirstOrDefault();

            if(entity == null)
            { 
                entity = new Operation();
                _ctx.Operations.InsertOnSubmit(entity);
            }

            entity.Name = model.Name;
            entity.ReturnType = model.ReturnType.FullName;
            entity.InterfaceID = model.Interface.ID;
            return entity;
        }
    }
}
